package com.atguigu.gulimail.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.gulimail.member.exception.PhoneExistException;
import com.atguigu.gulimail.member.exception.UserNameExistException;
import com.atguigu.gulimail.member.feign.CouponFeignService;
import com.atguigu.gulimail.member.vo.LoginVo;
import com.atguigu.gulimail.member.vo.RegistVo;
import com.atguigu.gulimail.member.vo.UserVo;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimail.member.entity.MemberEntity;
import com.atguigu.gulimail.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 会员
 *
 * @author songshaoqiu
 * @email songshaoqiu@gmail.com
 * @date 2022-01-07 17:00:47
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @PostMapping("/auth/login")
    public R authlogin(@RequestBody UserVo userVo) throws Exception {

    //yes
           MemberEntity memberEntity= memberService.authlogin(userVo);

           if (memberEntity!=null){
               return R.ok().setDate(memberEntity);
           }else{
               return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USERNAME_PASSWORD_EXCEPTION.getMsg()+"hahaha");
           }

    }

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){

        MemberEntity memberEntity=memberService.login(loginVo);

        if (memberEntity!=null){

            return R.ok().setDate(memberEntity);
        }else{
            return R.error(BizCodeEnum.USERNAME_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.USERNAME_PASSWORD_EXCEPTION.getMsg());
        }
    }


    //注册
    @PostMapping("/regist")
    public R regist(@RequestBody RegistVo registVo){

        try {

            memberService.regist(registVo);
        }catch (UserNameExistException e){
        return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());

        }catch (PhoneExistException e){
        return R.error(BizCodeEnum.PHONE_CODE_EXCEPTION.getCode(), BizCodeEnum.PHONE_CODE_EXCEPTION.getMsg());
        }

        return R.ok();
    }


    //远程调用 coupon的方法
    @RequestMapping("/mbGetCoupon")
    public R mbGetCoupon() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张飞");
        R coupon = couponFeignService.getCoupon();
        return R.ok().put("member", memberEntity).put("coupon", coupon.get("couponEntity"));

    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
